package com.kevin.Code.DP;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author Vinlee Xiao
 * @Classname CombinationSumiv
 * @Description Leetcode 377. 组合总和 Ⅳ  动态规划 完全背包问题 思路和Leetcode 518
 * @Date 2021/10/9 21:26
 * @Version 1.0
 */
public class CombinationSumiv {

    /**
     * @param nums
     * @param target
     * @return
     */
    public int combinationSum4(int[] nums, int target) {

        int len = nums.length;

        if (len == 1) {
            if (target == nums[0]) {
                return 1;
            } else {
                return 0;
            }
        }

        //对nums进行排序
        Arrays.sort(nums);

        if (nums[0] > target) {
            return 0;
        }


        //1.明确完全背包问题的状态和转移条件
        //背包容量就是target 数字是无限的
        //2.初始化问题
        int[] dp = new int[target + 1];
//        Arrays.fill(dp, target + 1);
        //边界初始化
        dp[0] = 1;
        for (int i = 1; i <= target; i++) {

            for (int j = 0; j < len; j++) {

                if (nums[j] <= i) {

                    dp[i] = dp[i] + dp[i - nums[j]];
                }
            }
        }


        return dp[target];
    }

    public static void main(String[] args) {
        CombinationSumiv combinationSumiv = new CombinationSumiv();

        int[] nums = new int[]{1, 2, 3};

        int i = combinationSumiv.combinationSum4(nums, 4);
        System.out.println(i);
    }

}
